//
//  XDFileDownloadViewController.m
//  iphone.mobileoa
//
//  Created by 李亚春 on 13-6-4.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "XDFileDownloadViewController.h"
#import "FileDownloadModel.h"
#import "ASIHTTPRequest.h"
#import "KKProgressToolbar.h"
#import "ASINetworkQueue.h"
#import "FileDownloadTask.h"
#import "IOUtil.h"

@interface XDFileDownloadViewController ()
{
    NSMutableArray *FileArray;
    KKProgressToolbar *statusToolbar;
    NSString *folderguid;
    ASINetworkQueue *netWorkQueue;//创建一个队列
    BOOL isDownload;
    NSString *downloadtaskguid;
}
@end

@implementation XDFileDownloadViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"附件下载";
    FileArray = [intentParams objectForKey:@"FileArray"];
    folderguid = [intentParams objectForKey:@"folderguid"];
    [self.tv reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_tv release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setTv:nil];
    [super viewDidUnload];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"simplecell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]init];
    }
    
    FileDownloadModel *m = [FileArray objectAtIndex:indexPath.row];
    cell.textLabel.text = m.fileName;
    [cell.textLabel setFont:[UIFont systemFontOfSize:12]];
    cell.accessoryType = UITableViewCellSelectionStyleBlue;
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return FileArray.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FileDownloadModel *m = [FileArray objectAtIndex:indexPath.row];
    
    //文件存在
    NSString *savePath = [NSString stringWithFormat:@"%@/Documents/attaches/%@/%@",NSHomeDirectory(),folderguid,m.fileName];
    NSError *error = nil;
    NSDictionary* dictFile = [[[NSFileManager defaultManager] attributesOfItemAtPath:savePath error:&error] retain];
    long nFileSize = [dictFile fileSize];
    if ([[NSFileManager defaultManager] fileExistsAtPath:savePath]&&nFileSize>0) {
        [self openFileFromSecondApp:savePath];
        return;
    }
    
    NSMutableDictionary *params = self.getPassDictionary;
    [params setObject:m forKey:@"model"];
    [params setObject:folderguid forKey:@"folerguid"];
    [params setObject:self.view forKey:@"cview"];
    [self showMBPHUD];
    downloadtaskguid = [[[FileDownloadTask alloc]initWithContext:self andParams:params]startTask];

}

-(void)refreshUIWithThreadGuid:(NSString *)threadGuid andData:(NSObject *)obj
{
    [self hideMBPHUD];
    NSString *path = (NSString *)obj;
    [self openFileFromSecondApp:path];
}

//-(void)openfile:(NSString *)_path
//    {
//        UIDocumentInteractionController *documentController =
//        [UIDocumentInteractionController
//         interactionControllerWithURL:[NSURL fileURLWithPath:_path]];
//        documentController.delegate = self;
//        [documentController retain];
//        //documentController.UTI = @"com.microsoft.word.doc";
//        [documentController presentOptionsMenuFromRect:CGRectZero inView:self.view  animated:YES];
//    }
@end
